//
//  SCNMoneyGuideViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/11/17.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit
import WebKit

class SCNMoneyGuideViewController: SCNTableViewController {

    var collectionView: UICollectionView!
    
    let itemIdentifier1 = "SCNMoneyGuideCell1"
    let itemIdentifier2 = "SCNMoneyGuideCell2"
    
    var result = Dictionary<String, SCNMoneyGuideDataresultModels>()
    
    var dataresultObj: SCNMoneyGuideDataresultModels!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "理财指南"
        
        configurationUI()
        
        request()
    }

    // MARK: UI
    
    func configurationUI() {

        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: SCNConstants.SCREENWIDTH, height: SCNConstants.SCREENHEIGHT - SCNConstants.NavigationHeight)
        layout.minimumLineSpacing = 0
        
        collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: SCNConstants.NavigationHeight, width: SCNConstants.SCREENWIDTH, height: SCNConstants.SCREENHEIGHT - SCNConstants.NavigationHeight), collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.isPagingEnabled = true
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.bounces = false
        view.addSubview(collectionView)
        
        collectionView.register(SCNMoneyGuideCell1.self, forCellWithReuseIdentifier: itemIdentifier1)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: itemIdentifier2)
    }
    
    override func request() {
        super.request()
        
        SCNMoneyGuideViewModel().requestMoneyGuideManageMoneyIndex([:]) { result in
            self.result = result
            self.dataresultObj = result["a"]!
            self.collectionView.reloadData()
        }
    }
}

extension SCNMoneyGuideViewController: UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        switch indexPath.item {
        case 0:
            let item1 = collectionView.dequeueReusableCell(withReuseIdentifier: itemIdentifier1, for: indexPath) as! SCNMoneyGuideCell1
            item1.MoneyGuideTopClosures = { [weak self] key in
                self?.dataresultObj = self?.result[key]
                self?.collectionView.reloadData()
            }
            if let obj = dataresultObj {
              item1.setMoneyGuideTableViewCell1(obj, index: indexPath)
            }            
            return item1
        case 1:
            let item2 = collectionView.dequeueReusableCell(withReuseIdentifier: itemIdentifier2, for: indexPath)

            let webView = WKWebView(frame: CGRect.init(x: 0, y: 0, width: SCNConstants.SCREENWIDTH, height: SCNConstants.SCREENHEIGHT - SCNConstants.NavigationHeight))
            item2.addSubview(webView)
            webView.load(URLRequest(url: URL(string: "\(SCNRequestAPI.H5API)\(dataresultObj.url ?? "nil")")!))
            return item2
        default:
            return UICollectionViewCell()
        }
        
    }
}
